import { TypeFilters } from "@/interface/filters";

// 字典 value:text
export const getFiltersDictText = (data: TypeFilters.filter[]): ObjValueStr => {
    return utilFiltersDict(data, 'value', 'text');
}

// 字典 value:paraphrase
export const getFiltersDictParaphrase = (data: TypeFilters.filter[]): ObjValueNum => {
    return utilFiltersDict(data, 'paraphrase', 'value');
}

// 字典 value:color
export const getFiltersDictColor = (data: TypeFilters.filter[]): ObjValueStr => {
    return utilFiltersDict(data, 'value', 'color');
}

// 字典 value:label
export const getFiltersDictLabel = (data: TypeFilters.filter[]): ObjValueStr => {
    return utilFiltersDict(data, 'value', 'label');
}

// 字典 id:name
export const getFiltersDictApi = (data: ObjValueAny[]) => {
    return utilFiltersDict(data, 'id', 'name');
}

// 生成字典
export const utilFiltersDict = (data: ObjValueAny[], key: string, value: string): ObjValueAny => {
    const row: ObjValueAny = {};
    const _length = data.length;
    for (let i = 0; i < _length; i++) {
        const dataRow: ObjValueAny = data[i];
        if (dataRow[key]) {
            row[dataRow[key]] = dataRow[value];
        }
    }
    return row;
}


// 过滤对象数组（获取部分状态）
export const getIncludeFilters = <T>(data: ObjValueAny[] = [], ids: Array<number | string> = [], key?: string): T[] => {
    return utilFiltersData<T>(data, ids, 'include', key);
}
// 过滤对象数组（排除部分状态）
export const getExcludeFilters = <T>(data: ObjValueAny[] = [], ids: Array<number | string> = [], key?: string): T[] => {
    return utilFiltersData<T>(data, ids, 'exclude', key);
}

export const utilFiltersData = <T>(data: ObjValueAny[] = [], ids: Array<number | string> = [], type: string, key: string = 'value'): T[] => {
    const storeStatus: T[] = [];
    const _length = data.length;
    for (let i = 0; i < _length; i++) {
        const row: ObjValueAny = data[i];
        if (type === 'include') {
            if (ids.includes(row[key])) {
                storeStatus.push(row as T);
            }
        }
        if (type === 'exclude') {
            if (!ids.includes(row[key])) {
                storeStatus.push(row as T);
            }
        }
    }
    return storeStatus;
}